package com.cjxch.supermybatis.core.tools.query;

import com.cjxch.supermybatis.core.tools.LambdaFunctionUtil;
import com.cjxch.supermybatis.core.tools.SerializableFunction;

import java.util.function.Function;

/**
 * @author Caizongyou
 */
public class LambdaSmCriterion {

    /**
     * 查询键
     */
    private String             key;

    /**
     * 查询值
     */
    private Object             value;

    /**
     * 查询类型
     */
    private CriteriaType type;

    /**
     * 连接符
     */
    private CriteriaConnector connector;

    protected LambdaSmCriterion(){
    }

    /**
     * 相等
     *
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion eq(R column, Object value, CriteriaConnector connector) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.eq, connector);
    }

    /**
     * 相等
     *
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion ge(R column, Object value, CriteriaConnector connector) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.ge, connector);
    }

    /**
     * 小于等于
     * 
     * @param column
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion le(R column, Object value, CriteriaConnector connector) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.le, connector);
    }

    /**
     * 大于
     * 
     * @param column
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion gt(R column, Object value, CriteriaConnector connector) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.gt, connector);
    }

    /**
     * 小于
     * 
     * @param column
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion lt(R column, Object value, CriteriaConnector connector) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.lt, connector);
    }

    /**
     * 模糊匹配
     * 
     * @param column
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion like(R column, Object value, CriteriaConnector connector) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.like, connector);
    }

    /**
     * 为空
     * 
     * @param column
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion isNull(R column, CriteriaConnector connector) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), null, CriteriaType.isNull, connector);
    }

    /**
     * 不为空
     * 
     * @param column
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion isNotNull(R column, CriteriaConnector connector) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), null, CriteriaType.isNotNull, connector);
    }





    /**
     * in
     * 
     * @param column
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion in(R column, Object value, CriteriaConnector connector) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.in, connector);
    }


    /**
     * 相等
     *
     * @param column
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion eq(R column, Object value) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.eq);
    }

    /**
     * 大于等于
     *
     * @param column
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion ge(R column, Object value) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.ge);
    }

    /**
     * 小于等于
     *
     * @param column
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion le(R column, Object value) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.le);
    }

    /**
     * 大于
     *
     * @param column
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion gt(R column, Object value) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.gt);
    }

    /**
     * 小于
     *
     * @param column
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion lt(R column, Object value) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.lt);
    }

    /**
     * 模糊匹配
     *
     * @param column
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion like(R column, Object value) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.like);
    }

    /**
     * 为空
     *
     * @param column
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion isNull(R column) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), null, CriteriaType.isNull);
    }

    /**
     * 不为空
     *
     * @param column
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion isNotNull(R column) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), null, CriteriaType.isNotNull);
    }

    /**
     * in
     *
     * @param column
     * @param value
     * @return
     */
    public <T, R extends Function<T, Object>> SmCriterion in(R column, Object value) {
        return new SmCriterion(LambdaFunctionUtil.getFieldName((SerializableFunction<T, Object>)column), value, CriteriaType.in);
    }



    public String getKey() {
        return key;
    }

    public Object getValue() {
        return value;
    }

    public CriteriaType getType() { return type; }

    public CriteriaConnector getConnector() { return connector; }
}
